﻿using B.S.Ingredients.APIRead.Application.Command.Cuitva;
using B.S.Ingredients.Domain;
using B.S.Ingredients.ErrorCode;
using B.S.Ingredients.Infartructrue.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Ingredients.APIRead.Application.CommandHandler.CuitvaHandler
{
    public class CuitvaFtCommandHandler : IRequestHandler<CuitvaFtCommand, APIResult<CuitvaClass>>
    {
        public CuitvaFtCommandHandler(IBaseRepository<CuitvaClass> CuitvaRepository)
        {
            this.CuitvaRepository = CuitvaRepository;
        }

        public IBaseRepository<CuitvaClass> CuitvaRepository { get; }

        public async Task<APIResult<CuitvaClass>> Handle(CuitvaFtCommand request, CancellationToken cancellationToken)
        {
     
            var path =  CuitvaRepository.GetAll().FirstOrDefault(a => a.Productuins == request.Productuins);
            if(path==null)
            {
                return new APIResult<CuitvaClass>
                {
                    Code = ResultCode.error,
                    Mag = "",
                    Data = null
                };
            }
            else
            {
                return new APIResult<CuitvaClass>
                {
                    Code = ResultCode.success,
                    Mag = "",
                    Data = path
                };
            }
        }
    }
}
